package ar.edu.unsam.algo3.plantasvszombies.wicket;

import ar.edu.unsam.algo3.plantasvszombies.dominio.Fila;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Jardin;
import ar.edu.unsam.algo3.plantasvszombies.dominio.JardinZen;
import ar.edu.unsam.algo3.plantasvszombies.dominio.Planta;
import com.google.common.base.Objects;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XAjaxLink;
import org.uqbar.wicket.xtend.XListView;

@SuppressWarnings("all")
public class FilaPanel extends Panel {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private Fila fila;
  
  private FeedbackPanel feedbackPanel = new Function0<FeedbackPanel>() {
    public FeedbackPanel apply() {
      FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
      return _feedbackPanel;
    }
  }.apply();
  
  public FilaPanel(final String id, final Fila model) {
    super(id);
    CompoundPropertyModel<Fila> _asCompoundModel = this._wicketExtensionFactoryMethods.<Fila>asCompoundModel(this.fila);
    this.setDefaultModel(_asCompoundModel);
    CompoundPropertyModel<Fila> _compoundPropertyModel = new CompoundPropertyModel<Fila>(model);
    Form<Fila> _form = new Form<Fila>("formFila", _compoundPropertyModel);
    final Form<Fila> formFila = _form;
    this.fila = model;
    JardinZen _jardinZen = this.fila.getJardinZen();
    boolean _equals = Objects.equal(_jardinZen, null);
    if (_equals) {
      this.init();
    }
    this.agregarDatos(formFila);
    this.agregarTabla(formFila);
    this.agregarAcciones(formFila);
    this._wicketExtensionFactoryMethods.addChild(this, formFila);
  }
  
  public void init() {
    JardinZen _jardinZen = new JardinZen();
    this.fila.setJardinZen(_jardinZen);
    Jardin _jardin = new Jardin();
    this.fila.setJardin(_jardin);
  }
  
  public MarkupContainer agregarDatos(final Form<Fila> form) {
    MarkupContainer _xblockexpression = null;
    {
      JardinZen _jardinZen = this.fila.getJardinZen();
      String _tipo = _jardinZen.getTipo();
      Label _label = new Label("tipo", _tipo);
      this._wicketExtensionFactoryMethods.addChild(form, _label);
      Label _label_1 = new Label("lugaresDisponibles");
      this._wicketExtensionFactoryMethods.addChild(form, _label_1);
      this.feedbackPanel.setOutputMarkupId(true);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, this.feedbackPanel);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public MarkupContainer agregarTabla(final Form<Fila> form) {
    MarkupContainer _xblockexpression = null;
    {
      XListView<Planta> _xListView = new XListView<Planta>("plantas");
      final XListView<Planta> listView = _xListView;
      final Procedure1<ListItem<Planta>> _function = new Procedure1<ListItem<Planta>>() {
        public void apply(final ListItem<Planta> item) {
          Planta _modelObject = item.getModelObject();
          CompoundPropertyModel<Planta> _asCompoundModel = FilaPanel.this._wicketExtensionFactoryMethods.<Planta>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          Label _label = new Label("nombre");
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(item, _label);
          Label _label_1 = new Label("defensa");
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(item, _label_1);
          Label _label_2 = new Label("ataque");
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(item, _label_2);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("eliminar");
          final Procedure1<XAjaxLink<Object>> _function = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  Planta _modelObject = item.getModelObject();
                  FilaPanel.this.fila.eliminarPlanta(_modelObject);
                  target.add(form);
                }
              };
              it.setOnClick(_function);
            }
          };
          XAjaxLink<Object> _doubleArrow = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function);
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(item, _doubleArrow);
        }
      };
      listView.setPopulateItem(_function);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public MarkupContainer agregarAcciones(final Form<Fila> form) {
    MarkupContainer _xblockexpression = null;
    {
      XListView<Planta> _xListView = new XListView<Planta>("jardinZen.plantines");
      final XListView<Planta> listView = _xListView;
      final Procedure1<ListItem<Planta>> _function = new Procedure1<ListItem<Planta>>() {
        public void apply(final ListItem<Planta> item) {
          Planta _modelObject = item.getModelObject();
          CompoundPropertyModel<Planta> _asCompoundModel = FilaPanel.this._wicketExtensionFactoryMethods.<Planta>asCompoundModel(_modelObject);
          item.setModel(_asCompoundModel);
          XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("seleccionar");
          final Procedure1<XAjaxLink<Object>> _function = new Procedure1<XAjaxLink<Object>>() {
            public void apply(final XAjaxLink<Object> it) {
              final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
                public void apply(final AjaxRequestTarget target) {
                  Planta _modelObject = item.getModelObject();
                  FilaPanel.this.agregarPlanta(target, _modelObject, form);
                }
              };
              it.setOnClick(_function);
            }
          };
          final XAjaxLink<Object> miBoton = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function);
          Label _label = new Label("nombre");
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(miBoton, _label);
          FilaPanel.this._wicketExtensionFactoryMethods.addChild(item, miBoton);
        }
      };
      listView.setPopulateItem(_function);
      XAjaxLink<Object> _xAjaxLink = new XAjaxLink<Object>("salir");
      final Procedure1<XAjaxLink<Object>> _function_1 = new Procedure1<XAjaxLink<Object>>() {
        public void apply(final XAjaxLink<Object> it) {
          final Procedure1<AjaxRequestTarget> _function = new Procedure1<AjaxRequestTarget>() {
            public void apply(final AjaxRequestTarget target) {
              MarkupContainer _parent = FilaPanel.this.getParent();
              target.add(_parent);
            }
          };
          it.setOnClick(_function);
        }
      };
      XAjaxLink<Object> _doubleArrow = ObjectExtensions.<XAjaxLink<Object>>operator_doubleArrow(_xAjaxLink, _function_1);
      this._wicketExtensionFactoryMethods.addChild(form, _doubleArrow);
      MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(form, listView);
      _xblockexpression = (_addChild);
    }
    return _xblockexpression;
  }
  
  public void agregarPlanta(final AjaxRequestTarget target, final Planta planta, final Form<? extends Object> form) {
    try {
      this.fila.agregarPlanta(planta);
      target.add(form);
    } catch (final Throwable _t) {
      if (_t instanceof UserException) {
        final UserException e = (UserException)_t;
        String _message = e.getMessage();
        this.error(_message);
        target.add(this.feedbackPanel);
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
  }
}
